#pragma once

#ifndef ESL_W_H_
#define ESL_W_H_


#include "esl.h"
#include "Tablet.h"
#include "ExtFunc.h"


#define WM_STATUS_MD	WM_USER + 2000
#define WM_LOG_STATUS	WM_USER + 2010


//class CTablet;

class CAdoPtr;

class CEsl_W
{
public:
	CEsl_W()
		:m_bInit(false)
		//,m_port(0)
	{	
		memset(&m_esl_h,0, sizeof(esl_handle_t));
		//m_ado = new CAdoPtr;
		/*memset(m_szAddr,0,255);
		memset(m_szName,0,255);
		memset(m_szPw,0,255);*/

	}
	CEsl_W(const char* szaddr, const int& iport, const char* szname, const char* szpw,HWND hwnd=NULL)
		:m_bInit(false)
		,m_pParentWnd(hwnd)
		//, m_port(iport)
	{
		memset( &m_esl_h, 0, sizeof(esl_handle_t) );
		esl_status_t t = esl_connect(&m_esl_h, szaddr, iport, szname, szpw);
		if( t == ESL_SUCCESS )
		{
			m_bInit = true;
		}
		//m_ado = new CAdoPtr;
	}
	~CEsl_W();

	int						init();
	esl_status_t			action(const char* str,...);
	inline	esl_handle_t	GetEsl_handle() { return m_esl_h; }
	void					QueryRegUser();
	void					QueryExtChannel();
	void					HandleEvent(esl_event_t* event=NULL);
protected:
	void					UIstatusFired(char* caller, char* callee, int& p_caller, int& p_callee, BOOL bt = TRUE );




private:
	esl_handle_t		m_esl_h;
	bool				m_bInit;
	HWND				m_pParentWnd;
	CAdoPtr*			m_ado;
};


static int thread_running=0;
static int running=1;
static int warn_stop =0;
static char* filter_uuid=NULL;


static void _sleep_ns(int secs, long nsecs) {

	Sleep(secs*1000 + nsecs/1000000);
}


static void sleep_ns(long nsecs) { _sleep_ns(0, nsecs); }
static void sleep_ms(int msecs) { sleep_ns(msecs*1000000); }
static void sleep_s(int secs) { _sleep_ns(secs, 0); }

extern int GetUserCount();


static void *msg_thread_run(esl_thread_t* p, void* obj)
{
		/*esl_handle_t *handle = (esl_handle_t *) obj;*/
		CEsl_W* pThis = (CEsl_W*) obj;
		esl_handle_t handle = pThis->GetEsl_handle();
		thread_running = 1;
		while(thread_running && handle.connected)
		{
			int aok =1;
			esl_status_t t = esl_recv_event_timed(&handle, 10, 1, NULL);
			if(t == ESL_FAIL)
			{
				esl_log(ESL_LOG_WARNING, "Disconnected.\n");
				thread_running = 0;
				running = -1;
			}
			else if(t == ESL_SUCCESS)
			{
				/*aok = stdout_writable();*/
				if(handle.last_event) {
					int known = 1;
					const char* type = esl_event_get_header(handle.last_event, "content-type");
					if(!esl_strlen_zero(type))
					{
						if(!strcasecmp(type,"log/data")) {
							const char* userdata = esl_event_get_header(handle.last_event, "user-data");
							if (esl_strlen_zero(userdata) || esl_strlen_zero(filter_uuid) || !strcasecmp(filter_uuid, userdata)){
								int level = 0;
								const char* lanme = esl_event_get_header(handle.last_event,"log-level");

								DWORD len = (DWORD)strlen(lanme);
								DWORD outbytes = 0;
								if(lanme)
									level = atoi(lanme);
							}
						}else if (!strcasecmp(type, "text/disconnect-notice")) {
							running = -1; thread_running = 0;
						} else if (!strcasecmp(type, "text/event-plain")) {
							//char *s;
							//const char *t =  esl_event_get_header(handle.last_ievent, "Event-Subclass");
							////if(t!=NULL)
							//	//UserStatus(handle.last_ievent, t);
							//esl_event_serialize(handle.last_ievent, &s, ESL_FALSE);
							//free(s);
							pThis->HandleEvent(handle.last_ievent);
						} else {
							known = 0;
						}
					}
					if (aok && !known) {
						char *s;
						esl_event_serialize(handle.last_event, &s, ESL_FALSE);
						free(s);
					}
				}
			}
			if (warn_stop) {
				warn_stop = 0;
			}
			sleep_ms(1);
		}

		thread_running = 0;
		return NULL;
}






#endif